package kuzmenkov.oip.service.impl;

import kuzmenkov.oip.common.I18nTool;
import kuzmenkov.oip.model.*;
import kuzmenkov.oip.service.ContactService;
import kuzmenkov.oip.service.ModuleChoosingService;
import kuzmenkov.oip.test.service.ContactServiceImplTest;
import org.junit.runner.JUnitCore;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;

/**
 * User: Michael Kuzmenkov
 * Date: 27.03.2009
 * Time: 15:25:23
 */
public class ContactServiceImpl implements ContactService {


    private static final Logger log = Logger.getLogger(ContactServiceImpl.class.getName());

    private ModuleChoosingService moduleChoosingService;
    private I18nTool i18nTool;

    public void setI18nTool(I18nTool i18nTool) {
        this.i18nTool = i18nTool;
    }

    public void setModuleChoosingService(ModuleChoosingService moduleChoosingService) {
        this.moduleChoosingService = moduleChoosingService;
    }

    public String getProducerName() throws Exception {
        return moduleChoosingService.getModuleProducerName();
    }

    public List<ContactItem> getContactItems() throws Exception {
        List<ContactItem> contactItems = null;
        try {
            Module module = moduleChoosingService.getCurrentModule();
            Producer producer = module.getProducer();
            Set<Contact> contacts = producer.getContacts();
            I18nTool itool = this.i18nTool;

            contactItems = new ArrayList<ContactItem>(contacts.size());
            for (Contact contact : contacts) {
                String contactName = itool.internationalizeMethod(contact, "getName");

                Set<Link> links = contact.getLinks();
                List<LinkItem> linkItems = new ArrayList<LinkItem>(links.size());
                for (Link link : links) {
                    String linkName = itool.internationalizeMethod(link, "getName");
                    String linkValue = itool.internationalizeMethod(link, "getValue");

                    LinkItem linkItem = new LinkItem(linkName, linkValue);
                    linkItems.add(linkItem);
                }

                ContactItem contactItem = new ContactItem(contactName, linkItems);
                contactItems.add(contactItem);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(i18nTool.getMessage("ContactServiceImpl.gettingContacts"));
        }
        return contactItems;
    }

    private Image getLogo() {
        Module module = moduleChoosingService.getCurrentModule();
        Producer producer = module.getProducer();
        Image logo = producer.getLogo();
        if (logo == null)
            log.warning("Producer haven't a logo");
        return logo;
    }

    public String getLogoImage() throws Exception {
        String filepath = null;
        try {
            Image logo = getLogo();
            if (logo == null)
                return null;
            filepath = logo.getFilePath();
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(i18nTool.getMessage("ContactServiceImpl.gettingLogo"));
        }
        return filepath;
    }

    public int getLogoWidth() throws Exception {
        int w = 0;
        try {
            Image logo = getLogo();
            if (logo == null)
                return 0;
            w = logo.getWidth();
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(i18nTool.getMessage("ContactServiceImpl.gettingImageWidht"));
        }
        return w;
    }

    public int getLogoHeight() throws Exception {
        int h = 0;
        try {
            Image logo = getLogo();
            if (logo == null)
                return 0;
            h = logo.getHeight();
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(i18nTool.getMessage("ContactServiceImpl.gettingImageHeight"));
        }
        return h;
    }
}
